﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Teris
{
    internal class Map:IDraw
    {
        List<DrawObject> walls;
        public List<DrawObject> dynamicWalls;
        //记录所有行的方块数目
        int[] recordInfo;
        int cubeNumber;

        public Map()
        { 
            walls = new List<DrawObject>();
            dynamicWalls = new List<DrawObject>();
            BuildWall();
            
        }

        public void Draw()
        {           
            //绘制固定墙
            for (int i = 0; i < walls.Count; i++)
            {
                walls[i].Draw();
                
            }   

            //绘制动态墙墙
            for (int i = 0; i < dynamicWalls.Count; i++)
            {
                dynamicWalls[i].Draw();
            }
        }

        void ClearDynamicWall()
        {
            for (int i = 0; i < dynamicWalls.Count; i++)
            {
                dynamicWalls[i].Clear();
            }
        }

        public void AddDynamicWall(List<DrawObject> w)
        {
            for (int i = 0; i < w.Count; i++)
            {
                if (w[i].pos.y <= 0)
                {
                    Game.ChangeState(GAME_STATE.END);
                    return;
                }

                w[i].ChangeType(E_DrawType.Wall);
                dynamicWalls.Add(w[i]);

                recordInfo[Game.h - 6  - w[i].pos.y]+=1;
            }

            ClearDynamicWall();
            //检测是否有需要删除的
            CheckClear();
            //绘制
            Draw();
        }

        void CheckClear()
        {
            List<DrawObject> delList = new List<DrawObject>();
            for (int i = 1; i < recordInfo.Length; i++)
            {
                //当某一行的方块数满时
                if (recordInfo[i] == cubeNumber)
                {
                    //遍历全部动态
                    for (int j = 0; j < dynamicWalls.Count; j++)
                    {
                        if (Game.h - 6  - dynamicWalls[j].pos.y == i)
                        {
                            delList.Add(dynamicWalls[j]);
                        }
                        else if (Game.h - 6 - dynamicWalls[j].pos.y > i)
                        {
                            ++dynamicWalls[j].pos.y;
                        }
                    }

                    
                    //删除掉需要剔除的方块
                    for (int k = 0; k < delList.Count; k++)
                    {
                        dynamicWalls.Remove(delList[k]);       
                    }

                    //更新目前的记录
                    for (int k = i; k < recordInfo.Length-1; k++)
                    {
                        recordInfo[k] = recordInfo[k + 1];
                    }
                    recordInfo[recordInfo.Length - 1] = 0;

                    //有可能出现连续剔除两行的情况，利用递归即可回溯为第一行重新计算
                    CheckClear();
                    break;
                }
            }
        }


        void BuildWall()
        {
            recordInfo = new int[Game.h - 6+1];
            cubeNumber = 0;
            for (int i = 0; i < Game.w; i += 2)
            {
                walls.Add(new DrawObject(E_DrawType.Wall, i, Game.h - 6));
                cubeNumber++;
            }
            cubeNumber -= 2;

            for (int i = 0; i < Game.h-6; i++)
            {
                walls.Add(new DrawObject(E_DrawType.Wall, 0, i));
                walls.Add(new DrawObject(E_DrawType.Wall, Game.w-2, i));
            }
        }
    }
}
